package com.lagou.work.task02.classloader;
import com.lagou.work.task03.angel.utils.IOUtil;

import java.io.*;
import java.lang.reflect.Method;
import java.net.Socket;

public class JobThread extends Thread{

    private Socket accept;

    public JobThread(Socket accept){
        this.accept = accept;
    }
    @Override
    public void run() {

        InputStream inputStream = null;
        PrintStream printStream = null;
        ByteArrayOutputStream byteArrOutStream = null;
        try {
            inputStream = accept.getInputStream();
            // 1 准备本地接收的输出流
            byteArrOutStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int pos;
            while ((pos = inputStream.read(buffer)) != -1){
                System.out.println(pos);
                byteArrOutStream.write(buffer,0, pos);
            }
            byteArrOutStream.flush();

            SelfDefineClassLoader classLoader = new SelfDefineClassLoader(byteArrOutStream.toByteArray());
            Class bubbleSortClass = classLoader.findClass("BubbleSort");
            Object obj = bubbleSortClass.getConstructor().newInstance();
            int[] arr ={1,6,7,3,23,8};
            Method sortMethod = bubbleSortClass.getMethod("sort", int[].class);
            int [] invoke = (int [])sortMethod.invoke(obj, arr);
            System.out.print("排序后的数组:");
            for (int i : invoke) {
                System.out.print(i+" ");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtil.close(accept, inputStream, byteArrOutStream, printStream);
        }
    }

}

